from pathlib import Path
import json

# print("welcome to files_test.py")
# print(Path(__file__))  # 最稳定的做法
# print(Path(__file__).parent)

project_root = Path(__file__).parent.parent
# path = project_root/'pi_digits.txt'
path = Path('pi_digits.txt')

# path = Path('../MvImport/pi_digits.txt') # 依赖运行环境

# contents = path.read_text()
# # contents = contents.rstrip()
# print(contents)

# lines = contents.splitlines()
# pi_string = ''
# for line in lines:
#     print(line)
#     pi_string += line.lstrip()
# print(pi_string)
# print(len(pi_string))

sal_root = Path(__file__).parent
new_path = sal_root/'test_pi_digits.txt'
new_path.write_text("I love programming.")

# 安全写入（指定编码，使用上下文管理器）
# with new_path.open('w', encoding='utf-8') as file:
with new_path.open('a', encoding='utf-8') as file:
    file.write("\nI do not love programming.")

# numbers = [2, 3, 5, 7, 11, 13]
# string = "I am a bird"
# json_path = sal_root/'numbers.json'
# contents = json.dumps(string)
# print(contents)
# json_path.write_text(contents)
# with json_path.open('a', encoding='utf-8') as file:
#     contents = json.dumps(string)
#     file.write(contents)